package com.tqw.es.controller;

import com.tqw.entity.Result;
import com.tqw.entity.StatusCode;
import com.tqw.es.pojo.Item;
import com.tqw.es.service.SearchService;
import com.tqw.es.vo.SearchVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Jim
 */
@RestController
@RequestMapping("/es")
public class SearchController {
    @Autowired
    private SearchService searchService;

    /**
     * 根据关键词搜索商品并返回
     * @param key 关键词
     * @param pageable 分页参数
     * @return 商品数据
     */
    @GetMapping("/searchItem")
    public Result<?> search(String key, Pageable pageable){
        List<Item> items = searchService.search(key, pageable);
        if (items==null) {
            return new Result<>(true, StatusCode.ERROR,"请输入关键词");
        }
        return new Result<>(true,StatusCode.OK,"成功",new SearchVO().setItems(items).setPageable(pageable));
    }
}
